package com.neusoft.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.neusoft.po.CommonResult;
import com.neusoft.service.AIService;

//@CrossOrigin("*")
@RefreshScope
@RestController
@RequestMapping("/AIController")
public class AIController {

	@Autowired
	private AIService aiService;
	
	@GetMapping("/AI/chat/{question}/{history}")
	public CommonResult<List> chat(@PathVariable("question") String question, @PathVariable("history") String history) throws Exception {
		System.out.println(question);
		System.out.println(history);

		Process proc;
//		Integer temp = Integer.valueOf(history);
		List<String> list = new ArrayList();
		try {
//			String str = aiService.getHistoryById(temp);
			String[] args1 = new String[] { "D:\\python\\python.exe",
					"gpt.py", question, history};
			proc = Runtime.getRuntime().exec(args1);
			BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(), "GBK"));
			StringBuffer buf = new StringBuffer();
			String line = null;
			while ((line = in.readLine()) != null) {
				buf.append(line);
			}
			String answer = buf.toString();
			System.out.println(answer);
			list.add(answer.split("&")[0]);
			list.add(answer.split("&")[1]);
			in.close();
			proc.waitFor();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return new CommonResult(200,"success（10300）",list);
	}
	
	@GetMapping("/AI/recommend/{question}")
	public CommonResult<List> recommend(@PathVariable("question") String question) throws Exception {
		System.out.println(question);

		Process proc;
		List<String> list = new ArrayList();
		try {
			String[] args1 = new String[] { "D:\\python\\python.exe",
					"gpt.py", question};
			proc = Runtime.getRuntime().exec(args1);
			BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(), "GBK"));
//			StringBuffer buf = new StringBuffer();
			String line = null;
			while ((line = in.readLine()) != null) {
//				buf.append(line);
//				buf.append("\n");
				list.add(line);
			}
//			String answer = buf.toString();
			System.out.println(list);
//			list.addAll(answer.split("\n"));
			in.close();
			proc.waitFor();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return new CommonResult(200,"success（10300）",list);
	}
}
